/**
 * File:		UserQuery.java
 * Project:		Virtual Advisor
 * Module:		Data Management
 * Author:		Rotem Ganel
 * Created:		Apr 30, 2011
 * Updated:		Apr 30, 2011
 * Version:		1 (in progress)
 */
package server.dataManagement.Queries;


public class UserTypeQuery extends Query {
	public static enum FIELD {
		IsAdmin("<admin username=\"", "\">"),
		IsStudent("<student username=\"", "\">");
	
		private String beginFlag;
		private String endFlag;
		
		/**
		 * Name:			FIELD
		 * Description:		constructs a new FIELD enumeration
		 * PreCondition:	the parameters are well-formed according to the structure of the related repository
		 * PostCondition:	a new FIELD enumeration is instantiated
		 * Parameters:		String beginFlag- the String to search for that precedes the requested data
		 * 					String endFlag- the String to search for that follows the requested data
		 * Notes:			flags are used to find data in the repository to fill the the Queries
		 */
		private FIELD(String beginFlag, String endFlag) {
			this.beginFlag = beginFlag;
			this.endFlag = endFlag;
		}
		
		/**
		 * Name:			beginFlag
		 * Description:		returns the beginFlag
		 * PreCondition:	the FIELD is well-formed
		 * PostCondition:	the beginFlag is returned
		 * Parameters:		none
		 * Notes:			none
		 */
		public String beginFlag() {
			return beginFlag;
		}
		
		/**
		 * Name:			endFlag
		 * Description:		returns the endFlag
		 * PreCondition:	the FIELD is well-formed
		 * PostCondition:	the endFlag is returned
		 * Parameters:		none
		 * Notes:			none
		 */
		public String endFlag() {
			return endFlag;
		}
		
		/**
		 * Name:			flags
		 * Description:		returns an array containing the related flags
		 * PreCondition:	the FIELD is well-formed
		 * PostCondition:	an array of flags is returned
		 * Parameters:		none
		 * Notes:			simple FIELDs will return a String[2]: beginFlag, endFlag
		 * 					complex FIELDs will return a String[4]: beginFlag, beginSubFlag, endSubFlag, endFlag
		 */
		public String[] flags() {
			return new String[]{beginFlag, endFlag};
		}
		
		/**
		 * Name:			
		 * Description:		
		 * PreCondition:	
		 * PostCondition:	
		 * Parameters:		
		 * Notes:			
		 */
		public boolean hasSubField() {
			return false;
		}
	}
	
	private static final String SEARCH_FLAG = "<users";
	private String username;
	
	/**
	 * Name:			
	 * Description:		
	 * PreCondition:	
	 * PostCondition:	
	 * Parameters:		
	 * Notes:			
	 */
	public UserTypeQuery(ACTION action, String username) {
		super(action, SEARCH_FLAG);
		this.username = username;
	}

	/**
	 * Name:			
	 * Description:		
	 * PreCondition:	
	 * PostCondition:	
	 * Parameters:		
	 * Notes:			
	 */
	//Duplicates the Query, but leaves the response blank
	public UserTypeQuery(UserTypeQuery original) {
		super(original);
		this.username = original.username;
	}
		
	/**
	 * Name:			
	 * Description:		
	 * PreCondition:	
	 * PostCondition:	
	 * Parameters:		
	 * Notes:			
	 */
	public String QUERY_TYPE() {
		return this.getClass().toString();
	}
	
	public String[][] flags() {
		String[][] flags = new String[FIELD.values().length][];
		
		for (int i = 0; i < FIELD.values().length; i++) {
			flags[i] = FIELD.values()[i].flags();
		}
		
		flags[0][0] += username;
		flags[1][0] += username;
		
		return flags;
	}
	
	public boolean respond(String[] response) {
		if (!(response[0] == null)) {
			response = new String[]{"Admin"};
		} else if (!(response[1] == null)) {
			response = new String[]{"Student"};
		} else {
			response = new String[]{"User DNE"};
		}
		
		return super.respond(response);
	}
}
